package com.lyz.redis.config;

import com.lyz.redis.service.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisAutoConfiguration {

    @Bean
    public <V> RedisTemplate<String,V> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String,V> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    @Bean
    public <V> StringRedisService<V> stringRedisService(RedisTemplate<String,V> redisTemplate){
        return new StringRedisService<>(redisTemplate);
    }

    @Bean
    public <V> ListRedisService<V> listRedisService(RedisTemplate<String,V> redisTemplate){
        return new ListRedisService<>(redisTemplate);
    }

    @Bean
    public <V> HashRedisService<V> hashRedisService(RedisTemplate<String,V> redisTemplate){
        return new HashRedisService<>(redisTemplate);
    }

    @Bean
    public <V> SortSetRedisService<V> sortSetRedisService(RedisTemplate<String,V> redisTemplate){
        return new SortSetRedisService<>(redisTemplate);
    }

    @Bean
    public <V> SetRedisService<V> setRedisService(RedisTemplate<String,V> redisTemplate){
        return new SetRedisService<>(redisTemplate);
    }

    @Bean
    public <V> StreamRedisService<V> streamRedisService(RedisTemplate<String,V> redisTemplate){
        return new StreamRedisService<>(redisTemplate);
    }
}
